﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Spider
{
    public partial class FrmTemplate : Form
    {
        public FrmTemplate()
        {
            InitializeComponent();
        }
        CompData compData = new CompData();
        XmlHelper xmlHelper = new XmlHelper(Environment.CurrentDirectory + "\\Template\\template.xml");

        public event EventHandler refreshFather = null;

        protected void OnRefreshData()
        {
            if (refreshFather != null)
            {
                refreshFather(this, EventArgs.Empty);
            }
        }
        int flag = 0; //0代表直接确定，1为新增，2为修改

        private void btnSure_Click(object sender, EventArgs e)
        {
            string tempid = string.Empty;
            switch (flag)
            {
                case 0:

                    break;
                case 1:
                    #region 新增模板
                    tempid = System.Guid.NewGuid().ToString();
                    xmlHelper.InsertElement("/Template", "Website", "tempid", tempid, "");
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "name", this.txtName.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "url", this.txtUrl.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "templateurl", this.txtTemplateUrl.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "urlreg", this.txtUrlReg.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "contentreg", this.txtContentReg.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "threadcount", this.txtThreadCount.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "encoding", this.cmboxCode.SelectedValue.ToString());
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "startstr", this.txtStartStr.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "endstr", this.txtEndStr.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "outtype", this.txtOutType.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "urlcontain", this.txtUrlContain.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "startpage", this.txtStartPage.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "endpage", this.txtEndPage.Text);
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "adddate", DateTime.Now.ToString());
                    xmlHelper.InsertElement("/Template/Website[@tempid='" + tempid + "']", "modifydate", DateTime.Now.ToString());
                    xmlHelper.Save();
                    compData.BindTemplate(this.cmboxTemplateList, tempid);
                    break;
                    #endregion
                case 2:
                    #region 编辑模板
                    tempid = this.cmboxTemplateList.SelectedValue.ToString();
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/name", this.txtName.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/url", this.txtUrl.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/templateurl", this.txtTemplateUrl.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/urlreg", this.txtUrlReg.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/contentreg", this.txtContentReg.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/threadcount", this.txtThreadCount.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/encoding", this.cmboxCode.SelectedValue.ToString());
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/startstr", this.txtStartStr.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/endstr", this.txtEndStr.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/outtype", this.txtOutType.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/urlcontain", this.txtUrlContain.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/startpage", this.txtStartPage.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/endpage", this.txtEndPage.Text);
                    xmlHelper.Replace("/Template/Website[@tempid='" + tempid + "']/modifydate", DateTime.Now.ToString());
                    xmlHelper.Save();
                    break;
                    #endregion
                default: break;
            }
            if (this.cmboxTemplateList.SelectedValue != null)
            {
                tempid = this.cmboxTemplateList.SelectedValue.ToString();
            }
            SpiderAccount.TemplateId = tempid;
            this.Close();
            OnRefreshData();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            flag = 1;
            foreach (Control c in this.panel1.Controls)
            {
                if (c.GetType().Name.Equals("TextBox"))
                {
                    c.Text = "";
                }
            }
            this.txtUrlReg.Text = "^/html(.+?).shtml$";
            this.txtContentReg.Text = "_[0-9]{1,2}\\.html$";
            this.txtOutType.Text = "gif|jpg|jpeg|png|bmp";
            this.txtThreadCount.Text = "1";
            this.txtStartPage.Text = "2";
            this.txtEndPage.Text = "5";
            this.txtName.Focus();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            flag = 2;
            this.txtName.Focus();
        }

        private void FrmTemplate_Load(object sender, EventArgs e)
        {
            #region 绑定编码
            this.cmboxCode.DataSource = CompData.GetRequestEnCode();
            this.cmboxCode.DisplayMember = "text";
            this.cmboxCode.ValueMember = "value";
            #endregion

            compData.BindTemplate(this.cmboxTemplateList, SpiderAccount.TemplateId);
            ShowTemplateData();
        }

        private void cmboxTemplateList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowTemplateData();
        }
        /// <summary>
        /// 显示文本框里的数据
        /// </summary>
        private void ShowTemplateData()
        {
            try
            {
                if (this.cmboxTemplateList.SelectedValue.ToString() != "")
                {
                    Hashtable ht = compData.GetTemplateData(this.cmboxTemplateList.SelectedValue.ToString());
                    foreach (Control c in this.panel1.Controls)
                    {
                        if (c.GetType().Name.Equals("TextBox"))
                        {
                            foreach (string name in ht.Keys)
                            {
                                if (c.Name.ToLower() == "txt" + name)
                                {
                                    c.Text = ht[name].ToString();
                                }
                            }
                        }
                    }
                    this.cmboxCode.SelectedValue = ht["encoding"].ToString();
                }
            }
            catch
            {
            }
        }
    }
}
